import Vue from "vue";
import Router from "vue-router";
import store from "@/store";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },

  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true,
  },

  {
    path: "/",
    component: Layout,
    redirect: "/student",
  },

  // 学生管理
  {
    path: "/student",
    component: Layout,
    redirect: "/student/list",
    name: "Vod",
    meta: { title: "学生管理", icon: "el-icon-s-order" },
    children: [
      {
        path: "list",
        name: "StudentList",
        component: () => import("@/views/student/list"),
        meta: { title: "学生列表", icon: "el-icon-user" },
      },
      {
        path: "add",
        name: "AddStudent",
        component: () => import("@/views/student/add"),
        meta: { title: "添加学生", icon: "el-icon-circle-plus-outline" },
      },
      {
        path: "edit/:id",
        name: "EditStudent",
        hidden: true,
        component: () => import("@/views/student/add"),
        meta: { title: "编辑", icon: "el-icon-edit" },
      },
    ],
  },
  // 教师管理
  {
    path: "/teacher",
    component: Layout,
    redirect: "/teacher/list",
    meta: { title: "教师管理", icon: "el-icon-s-help" },
    children: [
      {
        path: "list",
        name: "TeacherList",
        component: () => import("@/views/teacher/index"),
        meta: { title: "教师列表", icon: "el-icon-s-custom" },
      },
    ],
  },
  // 成绩管理
  {
    path: "/course",
    component: Layout,
    redirect: "/course/list",
    name: "Course",
    meta: { title: "课程成绩管理", icon: "el-icon-star-on" },
    children: [
      {
        path: "list",
        name: "CourseList",
        component: () => import("@/views/course/list"),
        meta: { title: "课程成绩列表", icon: "el-icon-document" },
      },
      {
        path: "add",
        name: "AddCourse",
        component: () => import("@/views/course/add"),
        meta: { title: "课程成绩录入", icon: "el-icon-thumb" },
      },
      {
        path: "test",
        name: "TestManage",
        component: () => import("@/views/course/test"),
        meta: { title: "课程管理", icon: "el-icon-help" },
      },
    ],
  },
  // 年级管理
  {
    path: "/class",
    component: Layout,
    redirect: "/class/list",
    meta: { title: "年级管理", icon: "el-icon-s-help" },
    children: [
      {
        path: "list",
        name: "ClassList",
        component: () => import("@/views/class/list"),
        meta: { title: "年级列表", icon: "el-icon-s-data" },
      },
    ],
  },
  // 系统设置
  {
    path: "/system",
    component: Layout,
    redirect: "/system/setting",
    meta: { title: "系统设置", icon: "el-icon-s-tools" },
    children: [
      {
        path: "setting",
        name: "SystemSetting",
        component: () => import("@/views/setting/password"),
        meta: { title: "修改密码", icon: "el-icon-lock" },
      },
      {
        path: "recover",
        name: "SystemRecover",
        component: () => import("@/views/setting/recover"),
        meta: { title: "找回密码", icon: "el-icon-unlock" },
      },
    ],
  },
  // 找回密码
  {
    path: "/password",
    hidden: true,
    component: () => import("@/views/setting/recover"),
    meta: { title: "找回密码", icon: "el-icon-eleme" },
  },

  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/404", hidden: true },
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

// 路由守卫 用于管理动态标签页 beforeEach是路由跳转前执行的钩子函数
router.beforeEach((to, from, next) => {
  // 获取动态标签页
  const editableTabs = store.state.tabs.editableTabs;

  // 如果当前标签页为空或者当前标签页不在动态标签页中 并且不是登录页
  if (
    editableTabs.length === 0 ||
    !editableTabs.some((item) => item.name === to.name)
  ) {
    if (to.path !== "/login") {
      // 添加动态标签页
      store.commit("tabs/setTableTabs", to);
    }
  }
  // 设置当前标签页
  store.commit("tabs/setTableTabsValue", to.path);
  next();
});

export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
